<script>

export default {
  name: 'HoutaiLogin',
  data () {
    return {
      ruleForm: {
        name: '',
        pass: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 8, max: 16, message: '长度在 8 到 16 个字符', trigger: 'blur' }
        ],
        pass: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 8, max: 16, message: '长度在 8 到 16 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          console.log(this.ruleForm)
          const res = await this.$axios.post(
            '/houtai/login/', { username: this.ruleForm.name, password: this.ruleForm.pass }, {
              headers: {
                'Content-Type': 'application/json' // 设置请求头
              }
            })
          localStorage.setItem('refresh-token', res.refresh)
          localStorage.setItem('access-token', res.access)
          if (localStorage.getItem('access-token')) {
            this.$router.push('/admin')
          }
        } else {
          alert('error submit!!')
          return false
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  }
}

</script>

<template>
  <div>
    <el-card class="box-card">
      <div slot="header">
        <span>登 录</span>
      </div>
      <div class="text item">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="用户名" prop="name">
            <el-input v-model="ruleForm.name"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="pass">
            <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')" style="background: #42b983">立即创建</el-button>
            <el-button @click="resetForm('ruleForm')" style="background: #42b983">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<style scoped>
.box-card {
  width: 450px;
  margin: 150px auto;

  .text {
    font-size: 14px;
  }

  .item {
    margin-bottom: 18px;
  }
}
</style>
